package com.woniu.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.order.app.utils.WebUtil;
import com.woniu.order.app.utils.WebUtilbak;
import com.woniu.order.client.OrderManageClient;
import com.woniu.order.client.param.AddOrderFlowingParam;
import com.woniu.order.client.param.OrderCreateParam;
import com.woniu.order.client.result.OrderFlowingResultInfo;
import com.woniu.order.infra.model.Order;
import com.woniu.order.service.OrderFlowingService;
import com.woniu.order.service.OrderService;
import com.woniu.order.service.dto.OrderFlowingDto;
import com.woniu.order.service.param.AddOrderFlowingServiceParam;
import com.woniu.order.service.param.OrderCreateServiceParam;
import com.woniu.order.service.param.OrderEntryServiceParam;
import com.woniu.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderManageApiImpl implements OrderManageClient {

    @Resource
    private OrderService orderService;

    @Resource
    private OrderFlowingService orderFlowingService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Resource
    private RocketMQTemplate rmt;

    /**
     * 创建订单
     *
     * @param orderCreateParam
     * @return
     */
    @Override
    public Result createOrder(OrderCreateParam orderCreateParam) throws Exception {
        log.info("创建订单：{}", orderCreateParam);
        OrderCreateServiceParam orderCreateServiceParam = BeanUtil.copyProperties(orderCreateParam, OrderCreateServiceParam.class);
        orderCreateServiceParam.setOrderEntryParams(BeanUtil.copyToList(orderCreateParam.getOrderEntryParams(), OrderEntryServiceParam.class));
        orderService.createOrder(orderCreateServiceParam);
        return Result.getInstance();
    }

    /**
     * 支付宝支付
     *
     * @param orderId
     * @return
     */
    @Override
    public void aliPay(Integer orderId) throws Exception {
        log.info("支付宝支付：{}", orderId);
        String result = orderService.aliPay(orderId);
        Order order = orderService.getById(orderId);
        if(result != null) {
            Message<String> msg = MessageBuilder.withPayload(order.getOrderNum()).build();
            rocketMQTemplate.syncSend("lxlg-find-pay", msg, 5000, 5);
            WebUtil.httpServletResponse().setCharacterEncoding("utf8");
            WebUtil.httpServletResponse().setContentType("text/html;charset=utf-8");
            WebUtil.httpServletResponse().getWriter().write(result);
            WebUtil.httpServletResponse().getWriter().flush();
        } else {
            WebUtilbak.getResponse().sendRedirect("errorpay.html");
        }
    }


    /**
     * 新增订单流水
     *
     * @param addOrderFlowingParam
     * @return
     */
    @Override
    public Result<OrderFlowingResultInfo> addOrderFlowing(AddOrderFlowingParam addOrderFlowingParam) {
        log.info("新增订单流水:{}", addOrderFlowingParam);
        AddOrderFlowingServiceParam addOrderFlowingServiceParam = BeanUtil.copyProperties(addOrderFlowingParam, AddOrderFlowingServiceParam.class);
        OrderFlowingDto orderFlowingDto = orderFlowingService.AddOrderFlowing(addOrderFlowingServiceParam);
        OrderFlowingResultInfo orderFlowingResultInfo = BeanUtil.copyProperties(orderFlowingDto, OrderFlowingResultInfo.class);
        return Result.getInstance(OrderFlowingResultInfo.class).setData(orderFlowingResultInfo);
    }
}
